﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
   public partial class wfProcPlaniMnt : EditPage
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         this.SetInitialTitle("Planilla");
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = ConstMultitabla.TIPO_MONEDA;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidMoneda, dt, "dsDescrip", "idCodigo", true, true);

      }

      public override void DataToControls()
      {
         EProcPlani objE = new EProcPlani();
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ProcPlani));
         objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objE.IdMes = Request.QueryString["IdMes"];
         objE.IdForPago = Request.QueryString["idForPago"];
         objE.IdPlanilla = Request.QueryString["IdPlanilla"];

         if (Request.QueryString["NuProcAnual"] != null)

            objE.NuProcAnual = Convert.ToInt16(Request.QueryString["NuProcAnual"]);

         objE = (EProcPlani)objBO.GetByKey(objE);


         if (objE != null)
         {

            ETablaGenDet objETabG = new ETablaGenDet();
            MGBOQry.ITablaGen objTabG = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

            objETabG.IdTabla = ConstMultitabla.MES;
            objETabG.IdCodigo = objE.IdMes;
            objETabG = (ETablaGenDet)objTabG.GetDetailByKey(objETabG);
            if (objETabG != null)
               WebHelper.EntityToControl(objETabG, "DsDescrip", txtidMes);

            objETabG.IdTabla = ConstMultitabla.FORMA_PAGO;
            objETabG.IdCodigo = objE.IdForPago;
            objETabG = (ETablaGenDet)objTabG.GetDetailByKey(objETabG);
            if (objETabG != null)
               WebHelper.EntityToControl(objETabG, "DsDescrip", txtidForPago);

            EPlanilla objPla = new EPlanilla();
            IBOQuery objBOPla = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.Planilla));
            objPla.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objPla.IdPlanilla = objE.IdPlanilla;
            objPla = (EPlanilla)objBOPla.GetByKey(objPla);
            if (objPla != null)
            {
               WebHelper.EntityToControl(objPla, "DsPlanilla", txtdsPlanilla);
               WebHelper.EntityToControl(objPla, "DsPlanilla", txtidPlanilla);
            }

            WebHelper.EntityToControl(objE, "IdMes", hdnidMes);
            WebHelper.EntityToControl(objE, "IdForPago", hdnidForPago);
            WebHelper.EntityToControl(objE, "IdPlanilla", hdnidPlanilla);
            WebHelper.EntityToControl(objE, "IdPlanilla", hdnidPlanilla);
            WebHelper.EntityToControl(objE, "NuProcAnual", txtNroProcAnual);
            WebHelper.EntityToControl(objE, "IdMoneda", ddlidMoneda);
            WebHelper.EntityToControl(objE, "MtTC", txtMtc);

            this.txtfeDesde.Text = objE.FeDesde.ToString("dd/MM/yyyy");
            this.TxtfeHasta.Text = objE.FeHasta.ToString("dd/MM/yyyy");
         }

      }

      public override void RegisterGeneralScripts()
      {
         this.txtdsPlanilla.Attributes.Add("readonly", "true");
         this.txtidForPago.Attributes.Add("readonly", "true");
         this.txtidMes.Attributes.Add("readonly", "true");
         this.txtfeDesde.Attributes.Add("readonly", "true");
         this.TxtfeHasta.Attributes.Add("readonly", "true");

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validateEditForm(key){");

         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtidPlanilla.ClientID + "','Seleccione Planilla');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.ddlidMoneda.ClientID + "','Seleccione Moneda');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtMtc.ClientID + "','Ingrese Monto Tipo Cambio(Mtc)');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

         StringBuilder sb2 = new StringBuilder();


         sb2.AppendLine("var varRet = modalDialog('wfPeriodoPlani.aspx',750,400);");
         sb2.AppendLine("if(varRet==null)");
         sb2.AppendLine("return false;");
         sb2.AppendLine("ctl = document.getElementById('" + this.hdnidForPago.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[0];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtidForPago.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[1];");
         sb2.AppendLine("ctl = document.getElementById('" + this.hdnidPlanilla.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[2];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtdsPlanilla.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[3];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtidPlanilla.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[3];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtNroProcAnual.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[4];");
         sb2.AppendLine("ctl = document.getElementById('" + this.hdnidMes.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[5];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtidMes.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[6];");
         sb2.AppendLine("ctl = document.getElementById('" + this.txtfeDesde.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[7];");
         sb2.AppendLine("ctl = document.getElementById('" + this.TxtfeHasta.ClientID + "');");
         sb2.AppendLine("ctl.value = varRet[8];");

         this.txtidPlanilla.OnClientClick = sb2.ToString();

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb2.ToString(), true);
      }

      public override void LockControls()
      {
         if (this.FormEditAction != FormEditAction.AddNew)
         {
            txtidPlanilla.EnabledButton = false;
         }
      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               this.Response.Redirect("wfProcPlaniBnd.aspx");
               break;
         }

      }

      public override void SaveData(String key)
      {
         if (this.FormEditAction == FormEditAction.AddNew && Convert.ToInt16(txtNroProcAnual.Value) != 1)
            if (this.ValidateData() != false)
               return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();

         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.ProcPlani));

         String[] strRet = objPro.UpdateData(objE);
         String strMsg = "";

         switch (this.FormEditAction)
         {
            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se ha registrado el Proceso : [" + this.txtNroProcAnual.Text + "]  de la Planilla :  [" + txtdsPlanilla.Text + "]";
               break;
            default:
               strMsg = "Se ha modificado el Proceso : [" + this.txtNroProcAnual.Text + "]  de la Planilla :  [" + txtdsPlanilla.Text + "]";
               break;
         }
         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "wfProcPlaniBnd.aspx");

      }

      public override bool ValidateData()
      {
         //EProcPlani objE = new EProcPlani();
         //IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ProcPlani));
         //objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         //objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         //objE.IdForPago = hdnidForPago.Value;
         //objE.IdPlanilla = hdnidPlanilla.Value;
         //objE.NuProcAnual = Convert.ToInt32(txtNroProcAnual.Value) - 1;
         //if (objBO.Exists(objE) == false)
         //{
         //   this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "No Puede crear esta planilla, porque no existe la Planilla inmediata anterior.", "wfProcPlaniBnd.aspx");
         //   return true;
         //}

         return false;
      }

      public override void DeleteData()
      {
         IEntityBase objE = (IEntityBase)this.CreateEntitySet();
         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.ProcPlani));
         int rpt = objPro.DeleteData(objE);
         if (rpt == 0)
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Este Proceso de Planilla no puede ser eliminado ya que cuenta con datos enlazados a otros procesos", "wfProcPlaniBnd.aspx");
         }
         else
         {
            this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, "Se anulo el Proceso : [" + this.txtNroProcAnual.Text + "]  de la Planilla :  [" + txtdsPlanilla.Text + "]", "wfProcPlaniBnd.aspx");
         }

      }

      public override Object CreateEntitySet()
      {
         EProcPlani objE = new EProcPlani();

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
            objE.EntityState = EntityState.Added;
         else
         {

            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ProcPlani));
            objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdMes = Request.QueryString["IdMes"];
            objE.IdForPago = Request.QueryString["idForPago"];
            objE.IdPlanilla = Request.QueryString["IdPlanilla"];
            objE.NuProcAnual = Convert.ToInt16(Request.QueryString["NuProcAnual"]);

            objE = (EProcPlani)objBO.GetByKey(objE);

            if (this.FormEditAction == FormEditAction.Edit)
               objE.EntityState = EntityState.Modified;
            if (this.FormEditAction == FormEditAction.Delete)
               objE.EntityState = EntityState.Deleted;

         }

         objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         WebHelper.ControlToEntity(hdnidMes, objE, "IdMes");
         WebHelper.ControlToEntity(hdnidForPago, objE, "IdForPago");
         WebHelper.ControlToEntity(hdnidPlanilla, objE, "IdPlanilla");
         WebHelper.ControlToEntity(txtNroProcAnual, objE, "NuProcAnual");
         WebHelper.ControlToEntity(ddlidMoneda, objE, "IdMoneda");
         WebHelper.ControlToEntity(txtMtc, objE, "MtTC");
         WebHelper.ControlToEntity(txtfeDesde, objE, "FeDesde");
         WebHelper.ControlToEntity(TxtfeHasta, objE, "FeHasta");
         objE.FeProceso = DateTime.Now;
         objE.StProceso = ConstEstadoProcePlani.ABIERTA;

         if (this.FormEditAction == FormEditAction.Delete)
         {
            objE.StAnulado = ConstFlagEstado.ACTIVADO;
         }
         else
         {
            objE.StAnulado = ConstFlagEstado.DESACTIVADO;
         }

         return objE;
      }



   }
}